
package xbean.__;

import java.io.StringReader;
import java.io.StringWriter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.goldhuman.Common.Marshal.OctetsStream;
import com.goldhuman.Common.Marshal.MarshalException;

public final class Token extends xdb.XBean implements xbean.Token {
	private long userid; // 

	@Override
	public void _reset_unsafe_() {
		userid = 0L;
	}

	Token(int __, xdb.XBean _xp_, String _vn_) {
		super(_xp_, _vn_);
	}

	public Token() {
		this(0, null, null);
	}

	public Token(Token _o_) {
		this(_o_, null, null);
	}

	Token(xbean.Token _o1_, xdb.XBean _xp_, String _vn_) {
		super(_xp_, _vn_);
		if (_o1_ instanceof Token) {
			assign((Token)_o1_);
		} else if (_o1_ instanceof Token.Data) {
			assign((Token.Data)_o1_);
		} else if (_o1_ instanceof Token.Const) {
			assign(((Token.Const)_o1_).nThis());
		} else {
			throw new UnsupportedOperationException();
		}
	}

	private void assign(Token _o_) {
		_o_._xdb_verify_unsafe_();
		userid = _o_.userid;
	}

	private void assign(Token.Data _o_) {
		userid = _o_.userid;
	}

	@Override
	public final OctetsStream marshal(OctetsStream _os_) {
		_xdb_verify_unsafe_();
		try {
			StringWriter sw = new StringWriter();
			JsonWriter _out_ = new JsonWriter(sw);
			marshal(_out_);
			_os_.marshalJson(sw.getBuffer().toString());
			_out_.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return _os_;
	}

	@Override
	public final void marshal(JsonWriter _out_) throws Exception {
		_out_.beginObject();
		if (userid != 0L) {
			_out_.name("1");
			_out_.value(this.userid);
		}
		_out_.endObject();
	}

	@Override
	public final OctetsStream unmarshal(OctetsStream _os_) {
		_xdb_verify_unsafe_();
		try {
			JsonReader jr = new JsonReader(new StringReader(_os_.unmarshalJson()));
			unmarshal(jr);
			jr.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return _os_;
	}

	@Override
	public final void unmarshal(JsonReader _in_) throws Exception {
		_in_.beginObject();
		while (_in_.hasNext()) {
			switch (_in_.nextName()) {
				case "1":
					this.userid = _in_.nextLong();
					break;
				default:
					_in_.skipValue();
					break;
			}
		}
		_in_.endObject();
	}

	@Override
	public xbean.Token copy() {
		_xdb_verify_unsafe_();
		return new Token(this);
	}

	@Override
	public xbean.Token toData() {
		_xdb_verify_unsafe_();
		xbean.__.Token.Data tmp = (Data) xbean.Pod.newTokenData();
		tmp.assign(this);
		return tmp;
	}

	public xbean.Token toBean() {
		_xdb_verify_unsafe_();
		return new Token(this); // same as copy()
	}

	@Override
	public xbean.Token toDataIf() {
		_xdb_verify_unsafe_();
		return new Data(this);
	}

	public xbean.Token toBeanIf() {
		_xdb_verify_unsafe_();
		return this;
	}

	@Override
	public xdb.Bean toConst() {
		_xdb_verify_unsafe_();
		return new Const();
	}

	@Override
	public long getUserid() { // 
		_xdb_verify_unsafe_();
		return userid;
	}

	@Override
	public void setUserid(long _v_) { // 
		_xdb_verify_unsafe_();
		if(userid == _v_) {
		    return;
		}
		xdb.Logs.logIf(new xdb.LogKey(this, "userid") {
			protected xdb.Log create() {
				return new xdb.logs.LogLong(this, userid) {
					public void rollback() {
						userid = _xdb_saved;
					}
					public boolean checkNoChange(Object v) {
						return (Long)v == _xdb_saved;
					}
				};
			}
		}, _v_);
		userid = _v_;
	}

	@Override
	public final boolean equals(Object _o1_) {
		_xdb_verify_unsafe_();
		Token _o_ = null;
		if ( _o1_ instanceof Token ) {
			_o_ = (Token)_o1_;
		} else if ( _o1_ instanceof Token.Const ) {
			_o_ = ((Token.Const)_o1_).nThis();
		} else {
			return false;
		}
		if (userid != _o_.userid) {
			return false;
		}
		return true;
	}

	@Override
	public final int hashCode() {
		_xdb_verify_unsafe_();
		int _h_ = 0;
		_h_ += userid;
		return _h_;
	}

	@Override
	public String toString() {
		_xdb_verify_unsafe_();
		StringBuilder _sb_ = new StringBuilder();
		_sb_.append("(");
		_sb_.append(userid);
		_sb_.append(")");
		return _sb_.toString();
	}

	@Override
	public xdb.logs.Listenable newListenable() {
		xdb.logs.ListenableBean lb = new xdb.logs.ListenableBean();
		lb.add(new xdb.logs.ListenableChanged().setVarName("userid"));
		return lb;
	}

	private class Const implements xbean.Token {
		Token nThis() {
			return Token.this;
		}

		@Override
		public void _reset_unsafe_() {
			throw new UnsupportedOperationException();
		}

		@Override
		public xbean.Token copy() {
			return Token.this.copy();
		}

		@Override
		public xbean.Token toData() {
			return Token.this.toData();
		}

		public xbean.Token toBean() {
			return Token.this.toBean();
		}

		@Override
		public xbean.Token toDataIf() {
			return Token.this.toDataIf();
		}

		public xbean.Token toBeanIf() {
			return Token.this.toBeanIf();
		}

		@Override
		public long getUserid() { // 
			_xdb_verify_unsafe_();
			return userid;
		}

		@Override
		public void setUserid(long _v_) { // 
			_xdb_verify_unsafe_();
			throw new UnsupportedOperationException();
		}

		@Override
		public xdb.Bean toConst() {
			_xdb_verify_unsafe_();
			return this;
		}

		@Override
		public boolean isConst() {
			_xdb_verify_unsafe_();
			return true;
		}

		@Override
		public boolean isData() {
			return Token.this.isData();
		}

		@Override
		public OctetsStream marshal(OctetsStream _os_) {
			return Token.this.marshal(_os_);
		}

		@Override
		public OctetsStream unmarshal(OctetsStream arg0) throws MarshalException {
			_xdb_verify_unsafe_();
			throw new UnsupportedOperationException();
		}

		@Override
		public void marshal(JsonWriter _out_) throws Exception {
			Token.this.marshal(_out_);
		}

		@Override
		public void unmarshal(JsonReader _in_) throws Exception {
			_xdb_verify_unsafe_();
			throw new UnsupportedOperationException();
		}

		@Override
		public xdb.Bean xdbParent() {
			return Token.this.xdbParent();
		}

		@Override
		public boolean xdbManaged() {
			return Token.this.xdbManaged();
		}

		@Override
		public String xdbVarname() {
			return Token.this.xdbVarname();
		}

		@Override
		public Long xdbObjId() {
			return Token.this.xdbObjId();
		}

		@Override
		public boolean equals(Object obj) {
			return Token.this.equals(obj);
		}

		@Override
		public int hashCode() {
			return Token.this.hashCode();
		}

		@Override
		public String toString() {
			return Token.this.toString();
		}

	}

	public static final class Data implements xbean.Token {
		private long userid; // 

		@Override
		public void _reset_unsafe_() {
			userid = 0L;
		}

		public Data() {
		}

		Data(xbean.Token _o1_) {
			if (_o1_ instanceof Token) {
				assign((Token)_o1_);
			} else if (_o1_ instanceof Token.Data) {
				assign((Token.Data)_o1_);
			} else if (_o1_ instanceof Token.Const) {
				assign(((Token.Const)_o1_).nThis());
			} else {
				throw new UnsupportedOperationException();
			}
		}

		private void assign(Token _o_) {
			userid = _o_.userid;
		}

		private void assign(Token.Data _o_) {
			userid = _o_.userid;
		}

	@Override
	public final OctetsStream marshal(OctetsStream _os_) {
		try {
			StringWriter sw = new StringWriter();
			JsonWriter _out_ = new JsonWriter(sw);
			marshal(_out_);
			_os_.marshalJson(sw.getBuffer().toString());
			_out_.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return _os_;
	}

	@Override
	public final void marshal(JsonWriter _out_) throws Exception {
		_out_.beginObject();
		if (userid != 0L) {
			_out_.name("1");
			_out_.value(this.userid);
		}
		_out_.endObject();
	}

	@Override
	public final OctetsStream unmarshal(OctetsStream _os_) {
		try {
			JsonReader jr = new JsonReader(new StringReader(_os_.unmarshalJson()));
			unmarshal(jr);
			jr.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return _os_;
	}

	@Override
	public final void unmarshal(JsonReader _in_) throws Exception {
		_in_.beginObject();
		while (_in_.hasNext()) {
			switch (_in_.nextName()) {
				case "1":
					this.userid = _in_.nextLong();
					break;
				default:
					_in_.skipValue();
					break;
			}
		}
		_in_.endObject();
	}

		@Override
		public xbean.Token copy() {
			return new Data(this);
		}

		@Override
		public xbean.Token toData() {
			xbean.__.Token.Data tmp = (Data) xbean.Pod.newTokenData();
			tmp.assign(this);
			return tmp;
		}

		public xbean.Token toBean() {
			return new Token(this, null, null);
		}

		@Override
		public xbean.Token toDataIf() {
			return this;
		}

		public xbean.Token toBeanIf() {
			return new Token(this, null, null);
		}

		// xdb.Bean interface. Data Unsupported
		public boolean xdbManaged() {
			throw new UnsupportedOperationException();
		}
		public xdb.Bean xdbParent() {
			throw new UnsupportedOperationException();
		}
		public String xdbVarname()  {
			throw new UnsupportedOperationException();
		}
		public Long xdbObjId() {
			throw new UnsupportedOperationException();
		}
		public xdb.Bean toConst() {	
			throw new UnsupportedOperationException();
		}
		public boolean isConst() {
			return false;
		}
		public boolean isData() {
			return true;
		}

		@Override
		public long getUserid() { // 
			return userid;
		}

		@Override
		public void setUserid(long _v_) { // 
			userid = _v_;
		}

		@Override
		public final boolean equals(Object _o1_) {
			if (!(_o1_ instanceof Token.Data)) {
				return false;
			}
			Token.Data _o_ = (Token.Data) _o1_;
			if (userid != _o_.userid) {
				return false;
			}
			return true;
		}

		@Override
		public final int hashCode() {
			int _h_ = 0;
			_h_ += userid;
			return _h_;
		}

		@Override
		public String toString() {
			StringBuilder _sb_ = new StringBuilder();
			_sb_.append("(");
			_sb_.append(userid);
			_sb_.append(")");
			return _sb_.toString();
		}

	}
}
